#
# Copyright 2008-2010 Vadim Zhukov <persgray@gmail.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY AGAVA COMPANY ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AGAVA COMPANY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of the AGAVA Company.


#######################################
# Base class for all rotate schemes. A scheme defines set of rules
#  for rotating backup. Each scheme have makeKeepDecision(),
#  which recieves list of all backup dates and choose ones to save.
#  Backup dates should be in "YYYYMMDD" format.
# Common init parameter is number of items to be saved.

package RB::RotateScheme;
use strict;

sub new($) {
	my $className = shift;
	my $this = {};
	$this->{'_maxLeft'} = shift;
	bless($this, $className);
	return $this;
}

sub maxLeft($) {
	my $this = shift;
	return $this->{'_maxLeft'};
}

# Arguments:
#   $this  - RB::RotateScheme object reference
#   @dates - list of dates to test
#
# Returns:
#   - list of dates of archives to be kept
sub makeKeepDecision($@) {
	my $this = shift;
	my $count = scalar(@_);
	return @_ if $count <= $this->{'_maxLeft'};
	# We always need to sort the data anyway
	my @sorted = sort { $b <=> $a } @_;
	return $this->_makeKeepDecision(@sorted);
}

# Calls _shouldUpdate() with $testDate and $hashSrc provided, which should
# return two dates describing period (inclusive) in which at least one existing
# archive date should part in.
#
# Arguments:
#   $this     - RB::RotateScheme object reference
#   $testDate - date to test
#   @dates    - list of dates to test
#
# Returns:
#   - boolean: true if we should refresh archive from scratch, false otherwise
sub shouldUpdate($$$@) {
	my ($this, $testDate, $hashSrc) = (shift, shift, shift);
	my $count = scalar(@_);
	return 1 if $count == 0;
	my ($start, $end) = $this->_shouldUpdate($testDate, $hashSrc);
	foreach my $d (@_) {
		if ($d >= $start and $d <= $end) {
			return 0;
		}
	}
	return 1;
}


1;
